/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import DragTable from '@/components/DragTable';
import { message, Popconfirm } from 'antd';
import { getPrintBusinessList, editLogin } from '@/services/print/business';

interface PrintBusiness {
  shop_id: string;
  name: string;
  account: string;
  phone: string;
  province: string;
  city: string;
  district: string;
  address: string;
  proxy_id: string;
  printer_num: string;
  forbid: number;
}

const Index: React.FC = () => {
  const actionRef = useRef<ActionType>();

  const getList = async (values: any) => {
    const { current: page, ...rest } = values;
    return getPrintBusinessList({ page, ...rest }).then((res) => {
      const { code, data, msg } = res;
      if (code == 0) {
        return {
          data: data.list,
          total: data.total,
        };
      } else {
        message.error(msg);
        return {
          data: [],
        };
      }
    });
  };
  const handleClick = (record: PrintBusiness) => {
    const { shop_id, forbid } = record;
    editLogin({ shop_id, forbid: forbid ? 0 : 1 }).then((res) => {
      const { code, msg } = res;
      if (code == 0) {
        actionRef.current?.reload();
        message.success('操作成功');
      } else {
        message.error(msg);
      }
    });
  };
  const getLoginStatus = (status: number) => {
    return status ? '允许' : '禁止';
  };
  const columns: ProColumns<PrintBusiness>[] = [
    {
      dataIndex: 'filter',
      hideInTable: true,
      fieldProps: {
        placeholder: '输入打印店名称/账号查询',
      },
    },
    {
      title: '打印店名称',
      dataIndex: 'name',
      hideInSearch: true,
      align: 'center',
      width: '15%',
    },
    {
      title: '管理员昵称',
      dataIndex: 'account',
      align: 'center',
      hideInSearch: true,
      width: '10%',
    },
    {
      title: '联系电话',
      dataIndex: 'phone',
      align: 'center',
      hideInSearch: true,
      width: '10%',
    },
    {
      title: '店铺地址',
      dataIndex: 'address',
      align: 'center',
      hideInSearch: true,
      width: '15%',
      render: (_, record) => {
        const { province, city, district, address } = record;
        return province + city + district + address;
      },
    },
    {
      title: '云文件打印代理id',
      dataIndex: 'proxy_id',
      align: 'center',
      hideInSearch: true,
      width: '15%',
    },
    {
      title: '开启打印机数',
      dataIndex: 'printer_num',
      align: 'center',
      hideInSearch: true,
      width: '10%',
    },
    {
      title: '昨日成功打印数',
      dataIndex: 'yesterday_print_num',
      align: 'center',
      hideInSearch: true,
      width: '15%',
    },
    {
      title: '操作',
      align: 'center',
      valueType: 'option',
      width: '10%',
      render: (_, record) => {
        const { forbid } = record;
        return (
          <Popconfirm
            title={`你确定${getLoginStatus(forbid)}登录吗？`}
            onConfirm={handleClick.bind(null, record)}
          >
            <a>{getLoginStatus(forbid)}登录</a>
          </Popconfirm>
        );
      },
    },
  ];

  return (
    <PageContainer>
      <DragTable>
        <ProTable
          rowKey='shop_id'
          actionRef={actionRef}
          request={getList}
          columns={columns}
          pagination={{
            pageSize: 20,
            showQuickJumper: false,
            showSizeChanger: false,
          }}
          scroll={{
            x: 900,
          }}
          options={false}
        />
      </DragTable>
    </PageContainer>
  );
};

export default Index;
